package com.hf.lookatme.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hf.lookatme.commons.DataConst;
import com.hf.lookatme.zbean.RegionData;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by JoeShiguang on 2015/5/23.
 */
public class DBExecutor {

  //public static DbUtils dbUtils;

  private static SQLiteDatabase db_region;

  /**
   * 获取DbUtils
   *
   * @param context
   *
   * @return
   */
  /*public static DbUtils instanceDbUtils(Context context) {

    if (dbUtils == null) {

      dbUtils = DbUtils.create(context, SDCardUtils.getSDCartPath() + DataConst.PATH_DATABASE, DataConst.DBNAME);
    }
    return dbUtils;
  }*/

  /**
   * 准备地区DataBase
   *
   * @param context
   */
  public static void prepareRegion(Context context) {

    String db_regionFilePath = SDCardUtils.getSDCartPath() + DataConst.PATH_DATABASE + "/" + DataConst.DBNAME_REGION;
    File file_db = new File(db_regionFilePath);
    if (file_db.exists()) {
      return;
    }
    InputStream stream = null;
    FileOutputStream outputStream = null;
    try {

      stream = context.getAssets().open("region");
      //将获取的stream流写入到data中
      outputStream = new FileOutputStream(file_db);
      byte[] buffer = new byte[400000];
      int count = 0;
      while ((count = stream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, count);
      }
      outputStream.close();
      stream.close();

      db_region = SQLiteDatabase.openOrCreateDatabase(file_db, null);
    } catch (Exception ex) {

      Logger.e(ex.getMessage());
    } finally {

      try {

        if (stream != null) {
          stream.close();
        }
        if (outputStream != null) {
          outputStream.close();
        }
        db_region.close();
      } catch (Exception ex) {

        Logger.e(ex.getMessage());
      }
    }
  }

  /**
   * 获取省份信息
   *
   * @return
   */
  public static List<RegionData> getProvinceData() {

    List<RegionData> list_province = null;
    String sql = "select * from v9_linkage_20150429 where parentId = 0";
    Cursor cursor = db_region.rawQuery(sql, null);
    try {

      list_province = new ArrayList<RegionData>();

      for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        RegionData province = new RegionData();
        province.setRegionId(cursor.getString(cursor.getColumnIndex("regionId")));
        province.setRegionName(cursor.getString(cursor.getColumnIndex("regionName")));
        province.setParentId(cursor.getString(cursor.getColumnIndex("parentId")));

        list_province.add(province);
      }
    } catch (Exception ex) {

      Logger.e(ex.getMessage());
    } finally {

      cursor.close();
      db_region.close();
    }
    return list_province;
  }

  /**
   * 获取城市信息
   *
   * @return
   */
  public static List<RegionData> getCityData(int provinceID) {

    List<RegionData> list_city = null;
    String sql = "select * from v9_linkage_20150429 where parentId = 1 and parentId = " + provinceID;
    Cursor cursor = db_region.rawQuery(sql, null);
    try {

      list_city = new ArrayList<RegionData>();

      for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        RegionData city = new RegionData();
        city.setRegionId(cursor.getString(cursor.getColumnIndex("regionId")));
        city.setRegionName(cursor.getString(cursor.getColumnIndex("regionName")));
        city.setParentId(cursor.getString(cursor.getColumnIndex("parentId")));

        list_city.add(city);
      }
    } catch (Exception ex) {

      Logger.e(ex.getMessage());
    } finally {

      cursor.close();
      db_region.close();
    }
    return list_city;
  }

  /**
   * 获取地区信息
   *
   * @return
   */
  public static List<RegionData> getAreaData(int cityID) {

    List<RegionData> list_area = null;
    String sql = "select * from v9_linkage_20150429 where parentId = 2 parentId = " + cityID;
    Cursor cursor = db_region.rawQuery(sql, null);
    try {

      list_area = new ArrayList<RegionData>();

      for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        RegionData city = new RegionData();
        city.setRegionId(cursor.getString(cursor.getColumnIndex("regionId")));
        city.setRegionName(cursor.getString(cursor.getColumnIndex("regionName")));
        city.setParentId(cursor.getString(cursor.getColumnIndex("parentId")));

        list_area.add(city);
      }
    } catch (Exception ex) {

      Logger.e(ex.getMessage());
    } finally {

      cursor.close();
      db_region.close();
    }
    return list_area;
  }

}
